package florestapokemon;

import java.awt.BorderLayout;
import java.awt.Graphics;

import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Image;
import javax.swing.ImageIcon;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.swing.Timer;

class Panel extends JPanel implements ActionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Graphics2D scene;
	private Image background;
	private Timer timer;
	private Intermediation intermediation;

	public Panel(){
		timer = new Timer(1000, this);
		intermediation = new Intermediation();
		background = new ImageIcon("res/wallpaper.jpg").getImage();
		timer.start();
	}

	public void paint(Graphics g){
		scene = (Graphics2D) g;
		scene.drawImage(background,0,0,null);
		
		Map<Position, Pokemon> state = intermediation.getState();
		Set<Position> pokemons = state.keySet();
		
		for (Iterator<Position> i = pokemons.iterator();i.hasNext();) {
            Position pos = i.next();
            Pokemon pokemon = state.get(pos);
            
            scene.drawImage(pokemon.getImage(),
                            64 * pos.x, 64 * pos.y, null);
		}
	}
	
	public void actionPerformed(ActionEvent e){
		intermediation.doIntermediation();
		updateUI();
	}
}

public class Scene extends JFrame{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public Scene(){
		super("Floresta Pokémon");
		add(new Panel(), BorderLayout.CENTER);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(640, 480);
		setLocationRelativeTo(null);
		setVisible(true);
		setResizable(false);
	}
}

